import sqlite3
import json

try:
    with open(r"C:\\Users\\P02PUBLIC\\Downloads\\chinese-poetry-master\\御定全唐詩\\json\\002.json", "r", encoding="utf-8") as file:
        data = json.load(file)
        # 连接到 SQLite 数据库（如果不存在则创建）
        conn = sqlite3.connect("C:\\Users\\P02PUBLIC\\Downloads\\chinese-poetry-master\\Ancient_Chinese_poetry.sqlite3")
        cursor = conn.cursor()
        #print(data[1]["volume"])
        # 插入数据
        for i in range(0, len(data)):
            cursor.execute("""
                INSERT INTO `main` ("id","book","preface","volume","part","chapter",
                "subchapter","section","subsection","type","title","subtitle",
                "rhythmic","subrhythmic","no#","author","biography","prologue",
                "short_description","description","abstract","source","dynasty",
                "paragraphs","contents","translation","notes","comments","tags",
                "spells","strains","origin")
                VALUES (NULL,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
            """, (
                "御定全唐诗",
                "",
                data[i]["volume"],
                "",
                "",
                "",
                "",
                "",
                "",
                data[i]["title"],
                "",
                "",
                "",
                data[i]["no#"],
                data[i]["author"],
                data[i]["biography"],
                "",
                "",
                "",
                "",
                "",
                "唐代",
                json.dumps(data[i]["paragraphs"]),
                "",
                "",
                json.dumps(data[i]["notes"]),
                "",
                "",
                "",
                "",
                ""

                ))

            # 提交事务并关闭连接
            conn.commit()
        conn.close()
        #print(data[1]['author'])
except json.JSONDecodeError as e:
    print("JSON 文件格式错误:", e)
except FileNotFoundError:
    print("文件未找到")

